<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Admin\Classify;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ClassController extends Controller
{
    /**
     * 添加分类
     *
     * @param Request $request
     * @return $this|array
     */
    public function add_class(Request $request)
    {

        if ($request->isMethod('get'))
        {
            $class = new Classify();
            $count  = $class->count();

            $list = DB::select('select * from bz_classify LEFT JOIN bz_tag ON bz_classify.tag_id = bz_tag.tag_id');
            //print_r($list);
            $tag_list = DB::select('select * from bz_tag');

            return view('Admin/Classify/add_class')->with('list',$list)->with('tag_list',$tag_list)->with('count',$count);
        }


        if ($request->isMethod('post'))
        {
            $data = $request->input();

            if (mb_strlen($data['name']) > 10 || mb_strlen($data['name']) < 2   )
            {
                return ['info'=>'请输入正确的分类名称，长度在2~10位之间','status'=>$data['name']];
            }

//            $re = '/[*&^%$#@!?\/]+/';
//            preg_match_all($re,$data['name'],$res);
//            //return ['s'=>$res,'name'=>$data['name']];
//            if (!empty($res))
//            {
//                return ['info'=>'分类名称不能带符号','status'=>$res];
//            }

            $tag_list = DB::select('select tag_id from bz_tag');
            $arr = [];
            foreach ($tag_list as $v )
            {
                $arr[] = $v->tag_id;
            }

            if (!in_array($data['tag_id'],$arr))
            {
                return ['info'=>'您的标签有误，请重试','status'=>0];
            }
            $class = new Classify();
            $class->tag_id = $data['tag_id'];
            $class->c_name = $data['name'];
            $res  = $class->save();
            if ($res)
            {
                return ['info'=>'成功','status'=>1];
            }else{
                return ['info'=>'失败','status'=>0];
            }

        }
    }


    /**
     * 修改分类名称
     *
     * @param Request $request
     * @param $id 分类id
     * @return $this|array
     */
    public function edit_class(Request $request,$id)
    {
        // get 请求
        if ($request->isMethod('get'))
        {
            $class = new Classify();
            $info = $class->find($id);
            $tag_list = DB::select('select * from bz_tag');
            return view('Admin/Classify/class_edit')->with('info',$info)->with('tag_list',$tag_list);
        }

        //  post 请求
        if ($request->method('post'))
        {
            $c_id = $request->input('c_id');
            $c_name = $request->input('name');
            $tag_id = $request->input('tag_id');

            if (mb_strlen($c_name) > 10 || mb_strlen($c_name) < 2   )
            {
                return ['info'=>'请输入正确的分类名称，长度在2~10位之间','status'=>0];
            }

            $re = '/^[*&^%\$#@!?]+$/';
            preg_match($re,$c_name,$res);
            if (!empty($res))
            {
                return ['info'=>'分类名称不能带符号','status'=>0];
            }

            $tag_list = DB::select('select tag_id from bz_tag');
            $arr = [];
            foreach ($tag_list as $v )
            {
                $arr[] = $v->tag_id;
            }

            if (!in_array($tag_id,$arr))
            {
                return ['info'=>'您的标签有误，请重试','status'=>0];
            }

            $class =  Classify::find($c_id);
            $class->tag_id = $tag_id;
            $class->c_name = $c_name;
            $res  = $class->save();
            if ($res)
            {
                return ['info'=>'成功','status'=>1];
            }else{
                return ['info'=>'失败','status'=>0];
            }

        }
    }


    /**
     * 删除分类
     *
     * @param Request $request
     * @param $id 分类id
     * @return array
     */
    public function class_del(Request $request,$id)
    {
        //
        if ($request->method('post'))
        {
            $class =  Classify::find($id);
            $res = $class->delete();

            if ($res)
            {
                return ['info'=>'成功','status'=>1];
            }else{
                return ['info'=>'失败','status'=>0];
            }

        }

    }
}












